package com.zztl.infiniteBand.util


import android.app.Activity
import com.zztl.infiniteBand.ui.activity.SettingActivity
import java.util.*

class ActivityManager private constructor() {

    val activityStack = Stack<Activity>()//activity栈

    //获取栈底的activity，先进后出原则
    val lastActivity: Activity?
        get() = activityStack.firstElement()

    //把一个activity压入栈中
    fun pushOneActivity(actvity: Activity) {
        activityStack.add(actvity)
    }

    //移除一个activity
    fun popOneActivity(activity: Activity?) {
        if (activityStack.size > 0) {
            if (activity != null) {
                activity.finish()
                activityStack.remove(activity)
            }

        }
    }


    //退出所有activity
    fun finishAllActivities() {

        while (activityStack.size > 0) {
            val activity = lastActivity ?: break
            if(activity is SettingActivity){
                break
            }
            popOneActivity(activity)

        }

    }

    fun finishAllToFirstActivities() {
        while (activityStack.size > 1) {
            val activity = lastActivity ?: break
            if (!activity.javaClass.simpleName.contains("MainActivity")) {
                popOneActivity(activity)
            }

        }

    }

    object SingleHoder {
        val instance = ActivityManager()
    }

    companion object {

        //单例模式
        fun getInstance(): ActivityManager {
            return SingleHoder.instance
        }
    }
}